# encoding: utf-8
__author__ = 'wwj'
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append("/Users/wwj/Downloads/shangtianjun/girl/")
from girl.dbShell.dbshell import dbshell

class main(dbshell):
    orderItem = [
            ['gid', ''],
            ['sid', ''],
            ['title', ''],
            ['movie', ''],
            ['thumb', ''],
            ['url', ''],
    ]
    existgirl = {}
    mongoTable = "movie"
    mysqltable = "movie"
    def __init__(self,spidername):
        super(main,self).__init__(spidername)
        self.geturltool("select distinct(url) from movie")
        self.getgirls()

    def getgirls(self):
        tmp = self.client[self.config.get('mongoDb')]["exitgirl"].find({},{"_id":0,"url":1})
        if tmp.count() == 0:
            tmp = self.getdb("select url,id,sid from girl")
            self.client[self.config.get('mongoDb')]["exitgirl"].insert_many([dict(url={i.split()[0]:(i.split()[1],i.split()[2])}) for i in tmp])
            tmp = self.client[self.config.get('mongoDb')]["exitgirl"].find({},{"url":1,"_id":0})
        for x in tmp:
            self.existgirl.update(x)

    def clean(self,y):
        data = self.data
        if not y.get("girlUrl"):
            raise self.myException("no girlurl")
        if not self.existgirl.get(y["girlUrl"]):
            raise self.myException("no girlexist")
        data["thumb"] = y["thumb"]
        data["movie"] = y["movie"]
        data["title"] = y["title"]
        data["url"] = y["url"]
        # data["gid"],data["sid"] = self.existgirl.get(y["girlUrl"])
        print data


if __name__ == '__main__':
    obj = main("city")
    obj()
